//
//  CommonTableViewCell.m
//  pushNews
//
//  Created by Jobs on 3/16/17.
//  Copyright © 2017 Iori. All rights reserved.
//

#import "CommonTableViewCell.h"
//#import "SpaceViewController.h"
#import "UIColor+Hex.h"
#import "Header.h"

@implementation CommonTableViewCell

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

-(void)setLabelSpace:(UILabel*)label withValue:(NSString*)str withFont:(UIFont*)font
{
    NSMutableParagraphStyle *paraStyle = [[NSMutableParagraphStyle alloc] init];
    paraStyle.lineBreakMode = NSLineBreakByTruncatingTail;
    paraStyle.alignment = NSTextAlignmentLeft;
    paraStyle.lineSpacing = 4; //设置行间距
    paraStyle.hyphenationFactor = 1.0;
    paraStyle.firstLineHeadIndent = 0.0;
    paraStyle.paragraphSpacingBefore = 0.0;
    paraStyle.headIndent = 0;
    paraStyle.tailIndent = 0;
    //设置字间距 NSKernAttributeName:@1.5f
    NSDictionary *dic = @{NSFontAttributeName:font,
                          NSParagraphStyleAttributeName:paraStyle,
                          NSKernAttributeName:@(0),
                          NSDocumentTypeDocumentAttribute:NSHTMLTextDocumentType
                          };
    
    NSAttributedString *attributeStr = [[NSAttributedString alloc] initWithData:[str dataUsingEncoding:NSUnicodeStringEncoding]
                                                                        options:dic
                                                             documentAttributes:NULL
                                                                          error:NULL];
    NSMutableAttributedString *abc = [attributeStr mutableCopy];
    [abc beginEditing];
    [abc addAttribute:NSFontAttributeName value:font range:NSMakeRange(0, abc.length)];
    [abc endEditing];
    
//    label.numberOfLines = 2;
    label.attributedText = abc;
    label.lineBreakMode = NSLineBreakByTruncatingTail;
}

-(void)updateContent:(NewsEntity *)news
{
    WS(weakSelf);
    self.imagePic01.contentMode = UIViewContentModeScaleAspectFill;
    self.imagePic02.contentMode = UIViewContentModeScaleAspectFill;
    self.imagePic03.contentMode = UIViewContentModeScaleAspectFill;
    self.imagePic04.contentMode = UIViewContentModeScaleAspectFill;
    self.labTag02.layer.borderColor = [UIColor whiteColor].CGColor;
    self.labTag02.layer.borderWidth = 0;
    self.labTag02.layer.cornerRadius = 0;
    self.labTitle.font = [UIFont systemFontOfSize:15];
    self.labTitle.numberOfLines = 2;
    self.btnName.titleLabel.font = [UIFont systemFontOfSize:14];
    //self.labTitle.text = news.art_title;
    [self setLabelSpace:self.labTitle withValue:news.art_title withFont:[UIFont systemFontOfSize:17]];
    self.imageHead.layer.cornerRadius = 10;
    [self.imageHead sd_setImageWithURL:[NSURL URLWithString:news.member.member_pic] placeholderImage:[UIImage imageNamed:@"nan1"] options:SDWebImageRetryFailed completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
        ;
    }];
    self.labName.text = [NSString stringWithFormat:@"%@[%@]", news.member.member_nickname, news.member.member_id];
    self.labVote.text = @"";
    self.labComment.text = news.art_desc;
    if(self.approvingOrApproved == 1)
    {
        self.labTag01.text = news.channel_name;
        self.labTag02.text = news.art_from;
    }
    else
    {
        self.labTag01.text = news.art_from;
        self.labTag02.text = @"";
    }

    
    if(news.art_img.count == 1)
    {
        [self.imagePic01 sd_setImageWithURL:[NSURL URLWithString:[news.art_img[0] objectForKey:@"path"]] placeholderImage:[UIImage imageNamed:@"iphone8.png"] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
            ;
        }];
        
    }
    else if(news.art_img.count > 1)
    {
        [self.imagePic01 setImage:nil];
        [self.imagePic02 setImage:nil];
        [self.imagePic03 setImage:nil];
        [self.imagePic04 setImage:nil];
        if(news.art_img.count > 1 )
        {
            [self.imagePic01 sd_setImageWithURL:[NSURL URLWithString:[news.art_img[0] objectForKey:@"path"]] placeholderImage:[UIImage imageNamed:@"iphone8.png"]];
            [self.imagePic02 sd_setImageWithURL:[NSURL URLWithString:[news.art_img[1] objectForKey:@"path"]] placeholderImage:[UIImage imageNamed:@"iphone8.png"]];
        }
        if(news.art_img.count > 2)
        {
            [self.imagePic03 sd_setImageWithURL:[NSURL URLWithString:[news.art_img[2] objectForKey:@"path"]] placeholderImage:[UIImage imageNamed:@"iphone8.png"]];
        }
        if(news.art_img.count > 3)
        {
            [self.imagePic04 sd_setImageWithURL:[NSURL URLWithString:[news.art_img[3] objectForKey:@"path"]] placeholderImage:[UIImage imageNamed:@"iphone8.png"]];
        }
    }

}

- (IBAction)btnName_click:(UIButton *)sender
{
    //[self.parentVC performSegueWithIdentifier:@"spaceSegue" sender:self.newsInfo.member];
//    SpaceViewController *vc = [UIStoryboard storyboardWithName:@"Space" bundle:nil].instantiateInitialViewController;
//    vc.memberInfo = self.newsInfo.member;
//    [self.parentVC.navigationController pushViewController:vc animated:YES];
}

static NSString* abc[] = {@"0x008fff",@"0xff9900", @"0x428d35"};
static NSInteger insetPadding1[] = {8,8,8};
static NSInteger insetPadding2[] = {8,8,8};

-(void)didTransitionToState:(UITableViewCellStateMask)state
{
    [super didTransitionToState:state];
    if ((state & UITableViewCellStateShowingDeleteConfirmationMask) == UITableViewCellStateShowingDeleteConfirmationMask)
    {
        self.subviews[0].backgroundColor = [UIColor clearColor];
        NSInteger iCount = 0;
        for (UIView *view in self.subviews[0].subviews)
        {
            view.backgroundColor = [UIColor colorWithPatternImage:[self getBackgroundColorImageWithBounds:CGRectMake(0, 0, view.frame.size.width, view.frame.size.height)
                                                                                                    color:[UIColor colorWithHexString: abc[iCount] alpha:.9]
                                                                                                insetLeft:insetPadding1[iCount]
                                                                                               insetRight:insetPadding2[iCount]
                                                                   ]];
            iCount ++;
        }
        
    }
}


-(UIImage*)getBackgroundColorImageWithBounds:(CGRect)bounds color:(UIColor*)color insetLeft:(CGFloat)inset1 insetRight:(CGFloat)inset2
{
    UIGraphicsBeginImageContextWithOptions(bounds.size, NO, 0);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, color.CGColor);
    bounds = CGRectMake(bounds.origin.x + inset1,
                        bounds.origin.y,
                        bounds.size.width-inset2,
                        bounds.size.height);
    UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:bounds cornerRadius:8];
    //     UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:imageView.bounds byRoundingCorners:UIRectCornerAllCorners cornerRadii:imageView.bounds.size];
    
    CGContextAddPath(context, path.CGPath);
    CGContextDrawPath(context, kCGPathFill);
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
}

#pragma mark - Image Generation -
+ (UIImage *)roundedCornerImageWithRadius:(CGFloat)radius color:(UIColor *)color
{
    UIImage *image = nil;
    
    // Make sure we have a valid color
    if (color == nil) { color = [UIColor whiteColor]; }
    
    // Rectangle if only one side is rounded, square otherwise
    CGRect rect = CGRectMake(0, 0, radius * 2, radius * 2);
    
    // Generation the image
    UIGraphicsBeginImageContextWithOptions(rect.size, NO, 0.0f);
    {
        UIBezierPath *bezierPath = [UIBezierPath bezierPathWithOvalInRect:rect];
        [color set];
        [bezierPath fill];
        image = UIGraphicsGetImageFromCurrentImageContext();
    }
    UIGraphicsEndImageContext();
    
    // Make the image conform to the tint color
    return image;
}


@end
